package org.arkeologgr.appening.adapter;

import java.util.List;

import org.arkeologgr.appening.domain.CultureItem;
import org.arkeologgr.appening.util.ImageLoader;

import org.arkeologgr.appening.R;
import org.arkeologgr.appening.R.id;
import org.arkeologgr.appening.R.layout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CultureItemListAdapter extends ArrayAdapter<CultureItem> {
	private List<CultureItem> itemList;
	private boolean mLoadMoreImages;

	public CultureItemListAdapter(Context context, int textViewResourceId,
			List<CultureItem> items) {
		super(context, textViewResourceId, items);
		this.itemList = items;
		mLoadMoreImages = true;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.list_item_cultureitem, null);
			ViewHolder holder = new ViewHolder();
			holder.thumb = (ImageView)  v.findViewById(R.id.itemlist_thumb);
			holder.title = (TextView) v.findViewById(R.id.itemtitle);
			holder.commentCount = (TextView) v.findViewById(R.id.itemcomment);
			v.setTag(holder);
		}
		CultureItem item = itemList.get(position);
		if (item != null) {
			ViewHolder holder = (ViewHolder) v.getTag();
			ImageView iv = holder.thumb;
			TextView tt = holder.title;
			if (iv != null) {
				
				//set default icon
				iv.setImageResource(R.drawable.tixel);
				//iv.setImageResource(R.drawable.ic_image_not_found);
                // set the actual image from the cache 
				if(item.getThumbUrl() != null && !item.getThumbUrl().equals("")){
					if (mLoadMoreImages) {
						ImageLoader.getImage(this, iv, item.getThumbUrl());
					} else {
						ImageLoader.getImage(iv, item.getThumbUrl(), false);
					}
				}
				

			}
			if (tt != null) {
				tt.setText(item.getTitle());
			}
			
			TextView tComment = holder.commentCount;
			if (tComment != null) {
				tComment.setText("");
			}
			
		}
		return v;
	}
	
	private static class ViewHolder {
		public ImageView thumb;
		public TextView title;
		public TextView commentCount;
	}
	
}
